package com.panshi.controller;

import com.panshi.entity.SysEssayCheckT;
import com.panshi.result.R;
import com.panshi.service.ZxClientService;
import com.panshi.utils.ZxInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class AdminZxController {

    @Autowired
    private ZxInfo zxInfo;

    @Autowired
    private ZxClientService zxClientService;

    @GetMapping("/zx/list")
    public String orderList(HttpServletRequest request, Model model, HttpSession session){
        zxInfo.initialize(request,model,session,"zxList");
        return "zx/list";
    }

    @GetMapping("/zx/view/{essayid}")
    public String orderView(@PathVariable int essayid,Model model) {
        System.out.println(essayid);
        SysEssayCheckT zxbyid = zxClientService.secById(essayid);
        model.addAttribute("sysEssayT",zxbyid);
        return "zx/view";
    }

    @GetMapping("/zx/add")
    public String orderAdd(){
        return "zx/add";
    }

    @GetMapping("/zx/upd")
    public String orderUpd(HttpServletRequest request, Model model, HttpSession session){
        zxInfo.initialize(request,model,session,"zxList");
        return "zx/upd";
    }

    @GetMapping("zx/updPoss/{sysEssayId}")
    public String updPoss(@PathVariable int sysEssayId,Model model){
        System.out.println(sysEssayId);

        model.addAttribute("sysEssayId",sysEssayId);
        return "zx/updPoss";
    }

    @GetMapping("zx/updNot/{sysEssayId}")
    public String updNot(@PathVariable int sysEssayId,Model model){
        System.out.println(sysEssayId);

        model.addAttribute("sysEssayId",sysEssayId);
        return "zx/updNot";
    }
    @PostMapping("zx/updNot/{sysEssayId}")
    @ResponseBody
    public R updNot(@PathVariable int sysEssayId,HttpServletRequest request){
        System.out.println(sysEssayId+"审核不通过");
        R r = new R();
        //描述
        String describe = request.getParameter("describe");
        if(sysEssayId==0 || describe ==null || describe.equals("")){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("信息错误");
            return r;
        }
        Boolean aBoolean = zxClientService.updNotAdd(sysEssayId, describe);
        if(aBoolean){
            r.setCode(201);
            r.setSuccess(false);
            r.setMessage("提交成功");
            return r;
        }
        return R.error();
    }
    //商品下架
    @GetMapping("/zx/delPer/{sysEssayId}")
    public String delPer(@PathVariable int sysEssayId,Model model){
        System.out.println(sysEssayId);
        model.addAttribute("sysEssayId",sysEssayId);
        return "zx/delPer";
    }
    //商品上架
    @GetMapping("/zx/delPut/{sysEssayId}")
    public String delPut(@PathVariable int sysEssayId,Model model){
        System.out.println(sysEssayId);
        model.addAttribute("sysEssayId",sysEssayId);
        return "zx/delPut";
    }
    @PostMapping("/zx/delPut/{sysEssayId}")
    @ResponseBody
    public R delPutadd(@PathVariable int sysEssayId,HttpServletRequest request){
        R r = new R();
        //描述
        String describe = request.getParameter("describe");
        if(sysEssayId==0 || describe ==null || describe.equals("")){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("信息错误");
            return r;
        }
        System.out.println(sysEssayId+"   "+describe);
        boolean aBoolean = zxClientService.essayPut(sysEssayId,describe);
        if(aBoolean){
            r.setCode(201);
            r.setSuccess(false);
            r.setMessage("咨询上架成功");
            return r;
        }
        return R.error();
    }

    @PostMapping("/zx/delPer/{sysEssayId}")
    @ResponseBody
    public R delPeradd(@PathVariable int sysEssayId,HttpServletRequest request){
        System.out.println(sysEssayId);
        R r = new R();
        //描述
        String describe = request.getParameter("describe");
        if(sysEssayId==0 || describe ==null || describe.equals("")){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("信息错误");
            return r;
        }
        boolean aBoolean = zxClientService.essayDel(sysEssayId, describe);
        if(aBoolean){
            r.setCode(201);
            r.setSuccess(false);
            r.setMessage("该咨询已下架");
            return r;
        }
        return R.error();
    }


    @PostMapping("/zx/updPoss/{sysEssayId}")
    @ResponseBody
    public R updPossAdd(@PathVariable int sysEssayId,HttpServletRequest request){
        System.out.println(sysEssayId);
        R r = new R();
        //描述
        String describe = request.getParameter("describe");
        if(sysEssayId==0 || describe ==null || describe.equals("")){
            r.setCode(301);
            r.setSuccess(false);
            r.setMessage("信息错误");
            return r;
        }
        Boolean aBoolean = zxClientService.updPossAdd(sysEssayId, describe);
        if(aBoolean){
            r.setCode(201);
            r.setSuccess(false);
            r.setMessage("审核通过");
            return r;
        }
        return R.error();
    }




    @GetMapping("/zx/del")
    public String orderDel(HttpServletRequest request, Model model, HttpSession session){
        zxInfo.initialize(request,model,session,"zxList");

        return "zx/del";
    }


    @PostMapping("/zx/upload")
    @ResponseBody
    public String zxUpload(@RequestParam("myFile") MultipartFile multipartFile, HttpServletRequest request){
        System.out.println(multipartFile);
        return "上传成功";

    }
}
